import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		tableData: [{
			name: '张三多大',
			id: Math.random().toFixed(10),
			age: 16,
			isAlone: true,
			account: 0,
			address: '湖南省长沙市岳麓区',
			iphone: 18711531111,
			userId: '430681198908250945',
			isInoculate: true
		}, {
			name: '张三a',
			id: Math.random().toFixed(10),
			age: 18,
			isAlone: false,
			account: 1,
			address: '湖南省长沙市s岳麓区',
			iphone: 18711531111,
			userId: '430681198908250945',
			isInoculate: true
		}]
	},
	getters: {
		isAloneNum(state){
			return state.tableData.reduce((total,item)=>item.isAlone?total+1:total,0)
		},
		isCityNum(state){
			return state.tableData.reduce((total,item)=>item.account?total+1:total,0)
		},
		isInoculateNum(state){
			return state.tableData.reduce((total,item)=>item.isInoculate?total+1:total,0)
		},
		adultNum(state){
			return state.tableData.reduce((total,item)=>item.age>=18?total+1:total,0)
		}
	},
	mutations: {
		addTableData(state,obj){
			obj.id =  Math.random().toFixed(10)
			state.tableData.push(obj)
		},
		upDateTableData(state,obj){
			state.tableData = state.tableData.map(item=>{
				if(item.id == obj.id){
					return obj
				}
				return item
			})
		},
		deleteTableData(state,idArr){
			console.log(state,idArr);
			state.tableData = state.tableData.filter(item=>{
				return   !idArr.includes(item.id)
			})
		}
	},
	actions: {},
	modules: {},
	plugins: [createPersistedState()]
})